<?php

namespace app\api\controller;

use app\api\model\User as UserModel;
use think\Db;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller {
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login() {
        $model = new UserModel;
        return $this->renderSuccess([
            'user_id' => $model->login($this->request->post()),
            'token'   => $model->getToken(),
        ]);
    }

    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail() {
        // 当前用户信息
        $userInfo = $this->getUser();
        return $this->renderSuccess(compact('userInfo'));
    }

    /*
     *收藏记录
     */

    public function get_collect() {
        // 当前用户信息
        $userInfo = $this->getUser();
        $collect  = Db::name('goods_collect')->alias('gc')
            ->join('goods g', 'g.goods_id=gc.goods_id')
            ->where('gc.user_id', $userInfo['user_id'])
            ->field('g.goods_id,g.goods_name')
            ->order('gc.create_time desc')
            ->paginate(10);

        $data = $collect->toArray();

        $data_collect = $data['data'];
        foreach ($data_collect as $key => $value) {
            $image_id = Db::name('goods_image')
                ->where('goods_id', $value['goods_id'])->order('id asc')->value('image_id');
            $file_name = Db::name('upload_file')
                ->where('file_id', $image_id)->value('file_name');
            $price = Db::name('goods_sku')
                ->where('goods_id', $value['goods_id'])->order('goods_price asc')->value('goods_price');
            $data_collect[$key]['goods_price'] = $price;
            $data_collect[$key]['checked']     = false;
            $data_collect[$key]['image_url']   = base_url() . 'uploads/' . $file_name;

        }

        return $this->renderSuccess([
            'list'    => $collect,
            'collect' => $data_collect,
        ]);

    }

    /*
     *收藏
     */
    public function collect_save() {
        $params   = $this->request->param();
        $userInfo = $this->getUser();
        $collect  = Db::name('goods_collect')->where('goods_id', $params['goods_id'])
            ->where('user_id', $userInfo['user_id'])
            ->find();
        if ($collect) {
            DB::name('goods_collect')->where('goods_id', $params['goods_id'])
                ->where('user_id', $userInfo['user_id'])
                ->delete();
        } else {
            $data['create_time'] = time();
            $data['goods_id']    = $params['goods_id'];
            $data['user_id']     = $userInfo['user_id'];
            Db::name('goods_collect')->insert($data);
        }

        return $this->renderSuccess([]);
    }

    /*
     *取消收藏
     */
    public function cancel_collect() {
        $params   = $this->request->param();
        $userInfo = $this->getUser();
        $collect  = json_decode(htmlspecialchars_decode($params['collect']), true);

        foreach ($collect as $key => $value) {
            if ($value['checked'] == 1) {

                DB::name('goods_collect')->where('goods_id', $value['goods_id'])
                    ->where('user_id', $userInfo['user_id'])
                    ->delete();
            }

        }
        return $this->renderSuccess([]);
    }

    /*
     *预约记录
     */

    public function get_yuyue() {
        // 当前用户信息
        $userInfo = $this->getUser();
        $list     = Db::name('user_store_yuyue')->alias('us')
            ->join('store_shop s', 's.shop_id=us.store_id')
            ->join('upload_file u', 'u.file_id=s.logo_image_id')
            ->where('us.user_id', $userInfo['user_id'])
            ->field('s.*,FROM_UNIXTIME(yuyue_date,"%Y-%m-%d") as yuyue_date_label,us.status,us.yuyue_time,u.file_name')
            ->order('us.id desc')
            ->paginate(10);

        return $this->renderSuccess([
            'list'     => $list,
            'base_url' => base_url() . 'uploads/',

        ]);

    }

    /**
     * 预约保存
     */
    public function yuyue_save() {
        $params   = $this->request->param();
        $userInfo = $this->getUser();

        $data['user_id']     = $userInfo['user_id'];
        $data['sex_id']      = $params['sex_id'];
        $data['phone']       = $params['phone'];
        $data['real_name']   = $params['name'];
        $data['age']         = $params['age'];
        $data['desc']        = $params['note'];
        $data['store_id']    = $params['store_id'];
        $data['create_time'] = time();

        Db::name('user_store_yuyue')->insert($data);
        return $this->renderSuccess([]);

    }

}
